#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = (StackDate*)malloc(sizeof(StackDate) * 4);
	if (ps->a == NULL)
	{
		printf("malloc fail\n");
		exit(0);
	}
	ps->capacity = 4;
	ps->top = 0;
}


void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
	ps = NULL;
}



//栈顶插入数据，入栈
void StackPush(ST* ps, StackDate x)
{
	assert(ps);
	if (ps->capacity==ps->top)
	{
		StackDate* tem = (StackDate*)realloc(ps->a, sizeof(StackDate) * 2 * ps->capacity);
		if (tem == NULL)
		{
			printf("realloc fail\n");
			exit(0);
		}
		else
		{
			ps->a = tem;
			ps->capacity = 2 * ps->capacity;
		}
	}

		ps->a[ps->top] = x;
		ps->top++;

}


//栈顶删除数据，出栈
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
		ps->top--;
}

//返回栈顶的元素
StackDate StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top-1];
}

//返回栈中元素的个数
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

//判断是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
